﻿using Microsoft.Extensions.Logging;

namespace System.Extensions
{
    public static class WriteLogExtensions
    {
        #region WriteLog
        /// <summary>
        /// 信息日志
        /// </summary>
        /// <param name="Logger">Logger</param>
        /// <param name="message">信息</param>
        /// <param name="args">参数</param>
        public static void WriteLog(this ILogger Logger, string message, params object[] args)
        {
            Logger.Log(LogLevel.Information, message.ToMessage(), args);
        }

        /// <summary>
        /// 级别信息
        /// </summary>
        /// <param name="Logger">Logger</param>
        /// <param name="level">级别</param>
        /// <param name="message">信息</param>
        /// <param name="args">参数</param>
        public static void WriteLog(this ILogger Logger, LogLevel level, string message, params object[] args)
        {
            Logger.Log(level, message.ToMessage(), args);
        }

        ///// <summary>
        ///// 带用户名的日志
        ///// </summary>
        ///// <param name="Logger">Logger</param>
        ///// <param name="level">级别</param>
        ///// <param name="creator">用户名</param>
        ///// <param name="message">信息</param>
        ///// <param name="args">参数</param>
        //public static void WriteLog(this ILogger Logger, LogLevel level, string creator, string message, params object[] args)
        //{
        //    object[] nArgs = new object[args.Length + 1];
        //    args.CopyTo(nArgs, 1);
        //    nArgs[0] = creator;
        //    //message = "{UserName} => " + message;

        //    Logger.Log(level, message.ToMessage(), nArgs);
        //}

        /// <summary>
        /// 写入异常日志
        /// </summary>
        /// <param name="Logger">ILogger</param>
        /// <param name="level">级别</param>
        /// <param name="ex">异常对象</param>
        /// <param name="messsage">信息</param>
        /// <param name="args">参数</param>
        public static void WriteLog(this ILogger Logger, LogLevel level, Exception ex, string messsage, params object[] args)
        {
            Logger.Log(level, ex, messsage.ToMessage(), args);
        }

        /// <summary>
        /// 带用户名的异常日志
        /// </summary>
        /// <param name="Logger">Logger</param>
        /// <param name="level">级别</param>
        /// <param name="creator">当前用户</param>
        /// <param name="ex">异常对象</param>
        /// <param name="message">日志信息</param>
        /// <param name="args">参数</param>
        public static void WriteLog(this ILogger Logger, LogLevel level, string creator, Exception ex, string message, params object[] args)
        {
            object[] nArgs = new object[args.Length + 1];
            args.CopyTo(nArgs, 1);
            nArgs[0] = creator;
            //message = "{UserName}> " + message;

            Logger.Log(level, ex, message, nArgs);
        }
        #endregion

    }
}
